﻿using System;
using System.Windows.Forms;
using Components;
using UI.Lib;
using UI.Lib.Factory;
using System.Collections;

namespace UI.BuyManagement
{
    public partial class FrmAcceptAdjust : UI.ParentForm.BaseMedit
    {
        string key = null;
        public FrmAcceptAdjust(string modulecode, string action, string pk = null)
            : base(modulecode, action)
        {
            InitializeComponent();

            this.key = pk;

            //回写的方法
            this.SaveCallFuntion = "Sto8a00Manager$MeditAcceptSaveData"; //保存的方法
            IsAfterSaveCloseWin = false;
            this.allowDtlEmpty = false;
            this.MaxStateFlag = true;//打开页面后让窗口最大化

            if (string.IsNullOrEmpty(this.key))
            {
                SetToolButton();
            }

            this.Text = "收货调整";

            LoadData();
        }

        //加载修改数据
        private void LoadData()
        {
            if (!string.IsNullOrEmpty(this.key))
            {
                base.LoadEditData(null, string.Format(" f.listid ='{0}' ", this.key));
                base.LoadDtlData(null, string.Format(" f.dtldocid ='{0}' ", this.key));
            }
        }

        protected override void PageInit()
        {
            this.tbSummary.DefaultWidth = 100;
            tspSave.Visible = true;
        }

        private void SetToolButton()
        {
            tspDtlTop.Items.Insert(0, CreateToolBtn("T", "提取源单据", btnGetData_Click));
        }

        private void btnGetData_Click(object sender, EventArgs e)
        {
            if (AutoCheck() == false)
                return;

            var orderListid = this.tbSummary.GetValue("refid");

            var where = QB.Entity.AddParam("@orderid", orderListid);

            //加载顶层节点
            var data = CommonHelper.Entity.ExecFun("getacceptdetails", where);

            BindEditData(data);
        }

        protected override bool BeforeDtlPopUp(string name, ref string where, ref string[] others)
        {
            if ("productcode" == name)
            {
                where = string.Format("a.status ='1' {0}", Context.GetAppKeyWhere("a.muser", true));
            }

            return true;
        }

        public override void AfterDtlPopWin(string name, Hashtable data, bool ischange)
        {
            {
                if ("productcode" == name)
                {
                    this.dgvDtl.SetValue("accqty", "0");
                    this.dgvDtl.SetValue("accpreqty", "0");
                    this.dgvDtl.SetValue("dtltotalamt", "0");
                    this.dgvDtl.SetValue("actqty1", "0");
                    this.dgvDtl.SetValue("actprice1", "0");
                    this.dgvDtl.SetValue("actqty2", "0");
                }
            }
        }

        public override bool BeforePopWin(string name, ref string where, ref string[] others)
        {
            if ("refdocno" == name)
            {
                where = string.Format(" a.ordstatus IN ('10') {0}", Context.GetAppKeyWhere("a.usercode", true));
            }

            return true;
        }

        protected override void AfterDtlValueChanged(string name, object value)
        {
            if ("actqty1" == name || "actprice1" == name) //计算收货金额
            {
                var qty = Validation.ToFloat(this.dgvDtl.GetValue("actqty1"));
                var ordprice = Validation.ToFloat(this.dgvDtl.GetValue("actprice1"));
                var all = Validation.ToDouble(qty * ordprice, 3);

                this.dgvDtl.SetValue("actamt1", all + all * Validation.ToDouble(this.dgvDtl.GetValue("cess")));

                CalcDtlQtyAndAmt();
            }
            else if ("actamt1" == name || name == "actqty2")
            {
                CalcDtlQtyAndAmt();
            }
        }

        private void CalcDtlQtyAndAmt()
        {
            decimal sumQty = 0;
            decimal sumAmt = 0;
            decimal cessamt = 0;
            foreach (DataGridViewRow row in this.dgvDtl.Rows)
            {
                var qty1 = Validation.ToFloat(row.Cells["accqty"].Value);
                var pqty1 = Validation.ToFloat(row.Cells["accpreqty"].Value);
                var qty2 = Validation.ToFloat(row.Cells["actqty1"].Value);
                var pqty2 = Validation.ToFloat(row.Cells["actqty2"].Value);
                var amt1 = Validation.ToFloat(row.Cells["accamt"].Value);
                var amt2 = Validation.ToFloat(row.Cells["actamt1"].Value);


                cessamt += Validation.ToFloat(row.Cells["cessamt"].Value);
                sumQty += (qty2 + pqty2 - (qty1 + pqty1));
                sumAmt += (amt2 - amt1);
            }

            this.tbSummary.SetValue("totalamt", sumAmt);
            this.tbSummary.SetValue("totalqty", sumQty);
        }
    }
}
